package com.ken.android.music.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ken.android.music.model.MusicBean;
import com.ken.android.music.R;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by axnshy on 16/5/21.
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private Context context;
    private List<MusicBean> data;
    private OnItemListener mLister;

    public MyAdapter(Context context, List<MusicBean> object) {
        this.context = context;
        data = object;
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        ImageView playIndicator;
        TextView musicName;
        TextView musicArtist;
        ImageView itemMenu;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            playIndicator = itemView.findViewById(R.id.iv_play_indicator);
            musicName = itemView.findViewById(R.id.id_music_name);
            musicArtist = itemView.findViewById(R.id.id_music_artist);
            itemMenu = itemView.findViewById(R.id.iv_item_menu);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_list_item,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final MusicBean music = data.get(position);
        holder.musicName.setText(music.getMusicName());
        if (music.getMusicArtist().length() > 20) {
            holder.musicArtist.setText(music.getMusicArtist().substring(1, 15) + "......");
        } else {
            holder.musicArtist.setText(music.getMusicArtist());
        }
        holder.playIndicator.setVisibility(View.INVISIBLE);
        holder.itemMenu.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if(mLister != null) {
                    mLister.onItemMenuClick(music);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data!= null?data.size():0;
    }

    public interface OnItemListener {
        void onItemMenuClick(MusicBean bean);
        void onItemClick();
    }

    public void setListener(OnItemListener listener){
        mLister = listener;
    }
}
